#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <stdint.h>
#include <pthread.h>

#include "audio_ns.h"
#include "testopt.h"
#include "audio_process.h"

#define AUDIO_BUF_SIZE 800

extern TEST_ARGS testArgs;
extern char fileName[NAME_BUF_SIZE];


static char inFileName[256] = "test_ns.pcm";
static char outFileName[256] = "test_ns_result.pcm";

static int sampleRate = 8000;

static int nsMode = 3;

static void *audio_NS_thread(void *argv)
{

	int ret = -1;
	int size = 0;

	char *srcName = inFileName;
	char *dstName = outFileName;

	FILE *fpSrc = fopen(srcName,"r");
	FILE *fpDst = fopen(dstName,"w");
	if(NULL == fpSrc || NULL == fpDst){
		printf("Open file %s or %s failed\n",srcName,dstName);
		return NULL;
	}
	NsHandle_t handle_ns;

	handle_ns.handle = audio_process_ns_create();
	if(0 == handle_ns.handle){
		printf("hanle_ns handle is NULL\n");
		return NULL;
	}

	ret = audio_process_ns_set_config(handle_ns.handle,sampleRate,nsMode);
	if(ret != 0) {
		printf("ns set config error.\n");
		return NULL;
	}
	switch(sampleRate){
	case 8000:
		handle_ns.cell_num = 80;
		break;
	case 16000:
	case 32000:
	case 48000:
		handle_ns.cell_num = 160;
		break;
	default:
		printf("Ns do not support samplerate: %d\n",sampleRate);
		return NULL;
		break;
	}
	handle_ns.sp = (float*)malloc(handle_ns.cell_num*sizeof(float));
	if(NULL == handle_ns.sp){
		printf("Ns_sp_malloc error.\n");
		return NULL;
	}
	memset(handle_ns.sp,0,handle_ns.cell_num*sizeof(float));
	handle_ns.out = (float*)malloc(handle_ns.cell_num*sizeof(float));
	if(NULL == handle_ns.out) {
		printf("Ns_out_malloc error\n");
		return NULL;
	}
	memset(handle_ns.out,0,handle_ns.cell_num*sizeof(float));

	printf("handle_ns.cell_num = %d\n",handle_ns.cell_num);

	unsigned char *pcm_buf = (unsigned char*)malloc(AUDIO_BUF_SIZE);

	if (NULL == pcm_buf){
		printf("malloc pcm_buf failed\n");
		return NULL;
	}
	while(1){
		size = fread(pcm_buf,1,AUDIO_BUF_SIZE,fpSrc);
		if (size <= 0)
			break;
		printf("size=%d\n",size);

		ret = Ns_Process(&handle_ns, (short*)(pcm_buf), (short*)pcm_buf,size);
		if (ret != 0){
			printf("Ns_Process failed\n");
			return NULL;
		}
		fwrite(pcm_buf,1,size,fpDst);
	}

	free(pcm_buf);
	free(handle_ns.sp);
	free(handle_ns.out);

	fclose(fpSrc);
	fclose(fpDst);

	pcm_buf = NULL;
	fpSrc = NULL;
	fpDst = NULL;
	return 0;
}

static void set_nsTest_param(TEST_ARGS *pTestArgs)
{
	if(NULL == pTestArgs) {
		printf("fun:%s,pTestArgs is empty.\n",__func__);
		return;
	}

	if(strcmp(pTestArgs->inFileName,"") != 0)
		strncpy(inFileName,pTestArgs->inFileName,MAX_FILE_NAME_LEN);

	if(strcmp(pTestArgs->outFileName,"") != 0)
		strncpy(outFileName,pTestArgs->outFileName,MAX_FILE_NAME_LEN);

	if(pTestArgs->nsMode >= 0)  /*set NS mode and flag*/
		nsMode = pTestArgs->nsMode;

}

int main(int argc,char *argv[])
{
	int ret = -1;

	ret = parse_param(argc,argv);
	if(1 == ret) {
		return 0;
	}

	if(ret < 0) {
		printf("parse_param failed\n");
		return -1;
	} else {
		printf("pase_param Ok\n");
	}

	set_nsTest_param(&testArgs);

	printf("This test parameter are as follows:\n\n");
	printf("inFileName = %s\n",inFileName);
	printf("outFileName = %s\n",outFileName);
	printf("sampleRate = %d\n",sampleRate);
	printf("nsMode = %d\n",nsMode);

	printf("[INFO]:Start to audio NS test\n");
	printf("[INFO]:Please input any key to continue.\n");
	getchar();

	pthread_t ns_thread_id;
	ret = pthread_create(&ns_thread_id,NULL,audio_NS_thread,NULL);
	if(ret != 0) {
		printf("create audio_NS_thread failed.\n");
		return -1;
	}

	pthread_join(ns_thread_id,NULL);

	printf("Finished ns thread.\n");

	return 0;

}

